%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   Aerodynamic Coefficient Calculation        
%   Author: Saurav Agarwal   
%   Email:  saurav6@gmail.com
%   Date:   January 1, 2011  
%   Place:  Dept. of Aerospace Engg., IIT Bombay, Mumbai, India 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [CD_fa,CD_fade,CL_fa,CL_fade,CY_fbdr,Cl_fada,Cm_fa,Cm_fade,Cn_fbdr,Cn_fada] = eval_aerod_coeff(de,da,dr,ALPHA,BETA);
CD_fa = eval_CD_fa(ALPHA);
CD_fade = eval_CD_fade(de,ALPHA);
CL_fa = eval_CL_fa(ALPHA);
CL_fade = eval_CL_fade(de,ALPHA);
CY_fbdr = eval_CY_fbdr(dr,BETA);
Cl_fada = eval_Cl_fada(da,ALPHA);
Cm_fa = eval_Cm_fa(ALPHA);
Cm_fade = eval_Cm_fade(de,ALPHA);
Cn_fbdr = eval_Cn_fbdr(dr,BETA);
Cn_fada = eval_Cn_fada(da,ALPHA);
end
function[CD_fa] = eval_CD_fa(alpha);

alpha1_table = linspace(-8,17,26);

% CD_fa
CD_falpha_lookup = [0.0734,0.0674,0.0629,0.0597, 0.0576,0.0568,0.0569,0.0580,0.0601,0.0630,0.0668,0.0713,0.0767,0.0830,0.0900,0.0980, 0.1069,0.1167,0.1276,0.1397,0.1530,0.2014,0.2209 0.1676,0.1837,0.2423];


CD_fa = interp1(alpha1_table,CD_falpha_lookup,alpha,'spline');

end
function[CD_fade] = eval_CD_fade(de,alpha)

% CD_fade
alpha2_table = linspace (-7,17,25);

alpha2_matrix = zeros(9,25);
elev_def = linspace(-20,20,9);

elev_matrix = zeros(9,25);

for i = 1:9
alpha2_matrix(i,:) = alpha2_table;
elev_matrix(i,:) = elev_def(i);
end;


cd_ade_neg20 = [0.0231,0.0209,0.0187,0.0166,0.0147,0.0129,0.0113,0.0098,0.0086,0.0074,0.0063,0.0053,0.0043,0.0031,0.0017,0.0001,-0.002,-0.0046,-0.0079,-0.012,-0.0171,-0.0234,-0.0311,-0.0403,-0.0543];

cd_ade_neg15 = [0.0178,0.0155,0.0135,0.0117,0.0101,0.0086,0.0073,0.0061,0.005,0.004,0.003,0.002,0.001,0,-0.001,-0.002,-0.0031,-0.0042,-0.0054,-0.0067,-0.008,-0.0094,-0.0109,-0.0125,-0.0141];

cd_ade_neg10 = [0.0111,0.0095,0.0081,0.0069,0.0057,0.0047,0.0038,0.003,0.0022,0.0015,0.0008,0.0001,-0.0006,-0.0013,-0.002,-0.0027,-0.0035,-0.0042,-0.005,-0.0058,-0.0067,-0.0075,-0.0084,-0.0093,-0.0102];

cd_ade_neg5 = [0.0058,0.005,0.0043,0.0037,0.0031,0.0025,0.002,0.0016,0.0011,0.0007,0.0003,-0.0001,-0.0005,-0.0009,-0.0013,-0.0018,-0.0022,-0.0027,-0.0032,-0.0037,-0.0043,-0.0048,-0.0054,-0.006,-0.0066];

cd_ade_0 = linspace(0,0,25);

cd_ade_5 = [-0.0028,-0.0021,-0.0015,-0.001,-0.0006,-0.0003,0,0.0003,0.0005,0.0007,0.001,0.0012,0.0014,0.0016,0.0018,0.002,0.0023,0.0025,0.0028,0.0031,0.0035,0.0038,0.0041,0.0045,0.0048];
	
cd_ade_10 = [-0.0053,-0.0037,-0.0024,-0.0014,-0.0005,0.0002,0.0008,0.0013,0.0018,0.0022,0.0026,0.003,0.0034,0.0039,0.0044,0.005,0.0056,0.0063,0.007,0.0078,0.0086,0.0095,0.0103,0.0111,0.0119];

cd_ade_15 = [-0.0063,-0.0043,-0.0025,-0.001,0.0002,0.0013,0.0022,0.0031,0.0039,0.0046,0.0053,0.006,0.0068,0.0076,0.0084,0.0093,0.0102,0.0112,0.0122,0.0133,0.0144,0.0155,0.0167,0.0177,0.0188];

cd_ade_20 = [-0.0029,-0.0007,0.0012,0.0028,0.0042,0.0053,0.0064,0.0074,0.0082,0.0091,0.0099,0.0108,0.0117,0.0126,0.0136,0.0147,0.0158,0.017,0.0182,0.0195,0.0209,0.0222,0.0236,0.025,0.0263];

CD_fade_lookup = [cd_ade_neg20;cd_ade_neg15;cd_ade_neg10;cd_ade_neg5;cd_ade_0;cd_ade_5;cd_ade_10;cd_ade_15;cd_ade_20];

CD_fade = griddata(elev_matrix,alpha2_matrix,CD_fade_lookup,de,alpha);

end
function[CL_fa] = eval_CL_fa(alpha);

% Aerodynamic Lookup Tables
alpha1_table = linspace(-8,17,26);

%CL_fa
CL_falpha_lookup = [-0.384,-0.294,-0.202,-0.107,-0.010,0.088,0.187,0.286,0.385,0.483,0.580,0.674,0.766,0.855,0.940,1.022,1.098,1.170,1.235,1.295,1.347,1.392,1.430,1.458,1.478,1.488];


CL_fa = interp1(alpha1_table,CL_falpha_lookup,alpha,'spline');

end
function[Cl_fada] = eval_Cl_fada(da,alpha)

% Cl_fada
alpha3_table = [-10.31,-8.22,-6.12,-4.01,-1.91,0.2,2.29	,4.4,6.5,8.59,10.65,12.71,13.72	,14.73,15.74,16.75,17.75,18.75,19.74,20.73];

ail_def = linspace(-25,25,11);

ail_matrix = zeros(11,20);
alpha3_matrix = zeros(11,20);

for i = 1:11
    ail_matrix(i,:) = ail_def(i);
    alpha3_matrix(i,:) = alpha3_table;
end

cl_ada_0 = linspace(0,0,20);
cl_ada_5 = [-0.014,-0.0143,-0.0148,-0.0148,-0.0153,-0.0158,-0.016,-0.016,-0.0152,-0.0144,-0.0128,-0.0118,-0.0109,-0.0105,-0.0093,-0.0093,-0.0087,-0.0073,-0.0061,-0.0016];
cl_ada_10 = [-0.028,-0.0281,-0.0292,-0.0299,-0.0296,-0.0299,-0.0308,-0.0308,-0.0295,-0.0279,-0.026,-0.0238,-0.0222,-0.0202,-0.0177,-0.0167,-0.0147,-0.0105,-0.0066,-0.0028];
cl_ada_15 = [-0.0366,-0.0375,-0.0386,-0.0408,-0.04,-0.0405,-0.0424,-0.0424,-0.0422,-0.0407,-0.0382,-0.0364,-0.0332,-0.0307,-0.028,-0.0267,-0.0238,-0.0179,-0.013,-0.0077];
cl_ada_20 = [-0.0446,-0.0463,-0.0482,-0.0484,-0.0475,-0.0482,-0.0519,-0.0519,-0.0527,-0.0521,-0.0495,-0.0466,-0.0437,-0.0411,-0.0388,-0.036,-0.0333,-0.0285,-0.0226,-0.017];
cl_ada_25 = [-0.0533,-0.0553,-0.0568,-0.0563,-0.0568,-0.0573,-0.0604,-0.0604,-0.0614,-0.0616,-0.0597,-0.0558,-0.0524,-0.0487,-0.0461,-0.0423,-0.0376,-0.0342,-0.028,-0.0229];
cl_ada_neg5 = -1*cl_ada_5;
cl_ada_neg10 = -1*cl_ada_10;
cl_ada_neg15 = -1*cl_ada_15;
cl_ada_neg20 = -1*cl_ada_20;
cl_ada_neg25 = -1*cl_ada_25;

Cl_fada_lookup = [cl_ada_neg25;cl_ada_neg20;cl_ada_neg15;cl_ada_neg10;cl_ada_neg5;cl_ada_0;cl_ada_5;cl_ada_10;cl_ada_15;cl_ada_20;cl_ada_25];

Cl_fada = griddata(ail_matrix,alpha3_matrix,Cl_fada_lookup,da,alpha);
end
function[CL_fade] = eval_CL_fade(de,alpha)

% CL_fade
alpha2_table = linspace (-7,17,25);
elev_def = linspace(-20,20,9);
elev_matrix = zeros(9,25);
alpha2_matrix = zeros(9,25);

for i=1:9
    elev_matrix(i,:) = elev_def(i);
    alpha2_matrix(i,:) = alpha2_table;
end;
    
cl_ade_neg20 = [-0.088,-0.093,-0.098,-0.103,-0.106,-0.11,-0.113,-0.116,-0.118,-0.119,-0.121,-0.122,-0.122,-0.123,-0.123,-0.122,-0.122,-0.121,-0.119,-0.118,-0.116,-0.114,-0.112,-0.109,-0.107];

cl_ade_neg15 = [-0.075,-0.079,-0.083,-0.087,-0.091,-0.094,-0.097,-0.1,-0.102,-0.105,-0.107,-0.108,-0.11,-0.111,-0.111,-0.111,-0.111,-0.11,-0.109,-0.108,-0.106,-0.103,-0.1,-0.097,-0.093];

cl_ade_neg10 = [-0.057,-0.061,-0.064,-0.067,-0.07,-0.072,-0.074,-0.076,-0.078,-0.079,-0.079,-0.08,-0.08,-0.08,-0.08,-0.079,-0.078,-0.077,-0.075,-0.073,-0.071,-0.068,-0.065,-0.062,-0.058];

cl_ade_neg5 = [-0.036,-0.038,-0.04,-0.041,-0.043,-0.044,-0.045,-0.045,-0.045,-0.045,-0.045,-0.045,-0.044,-0.043,-0.042,-0.041,-0.04,-0.038,-0.037,-0.035,-0.033,-0.031,-0.029,-0.027,-0.025];

cl_ade_0 = linspace(0,0,25);
	
cl_ade_5 = [0.025,0.026,0.027,0.027,0.028,0.029,0.03,0.03,0.031,0.031,0.032,0.032,0.032,0.032,0.032,0.032,0.032,0.032,0.031,0.03,0.029,0.028,0.027,0.025,0.024];

cl_ade_10 =[0.058,0.059,0.06,0.061,0.062,0.063,0.064,0.065,0.066,0.066,0.067,0.067,0.068,0.068,0.068,0.068,0.067,0.066,0.065,0.064,0.062,0.061,0.058,0.055,0.052];

cl_ade_15 = [0.093,0.093,0.094,0.095,0.096,0.097,0.098,0.099,0.099,0.1,0.1,0.1,0.1,0.1,0.099,0.098,0.096,0.094,0.092,0.089,0.086,0.082,0.078,0.073,0.068];

cl_ade_20 = [0.1,0.101,0.101,0.102,0.103,0.104,0.1060,0.107,0.108,0.11,0.111,0.112,0.113,0.113,0.114,0.113,0.113,0.111,0.11,0.107,0.104,0.1,0.096,0.09,0.084];

CL_fade_lookup = [cl_ade_neg20;cl_ade_neg15;cl_ade_neg10;cl_ade_neg5;cl_ade_0;cl_ade_5;cl_ade_10;cl_ade_15;cl_ade_20];

CL_fade = griddata(elev_matrix,alpha2_matrix,CL_fade_lookup,de,alpha);

end
function[Cm_fa] = eval_Cm_fa(alpha)

alpha = 1;
% Aerodynamic Lookup Tables
alpha1_table = linspace(-8,17,26);

% Cm_fa
Cm_falpha_lookup = [0.411,0.39,0.365,0.338,0.309,0.279,0.251,0.222,0.194,0.167,0.139,0.11,0.08,0.047,0.012,-0.027,-0.07,-0.116, -0.168,-0.223,-0.281,-0.342,-0.404,-0.465,-0.522,-0.573];

Cm_fa = interp1(alpha1_table,Cm_falpha_lookup,alpha);

end
function[Cm_fade] = eval_Cm_fade(de,alpha)


% Cm_fade
alpha4_table = [-6,-4,-2,0,2,4,6,8,10,12,13,14,15,16,17,18,19,20];
elev2_def = linspace(-25,20,10);

elev2_matrix = zeros(10,18);
alpha4_matrix = zeros(10,18);

for i= 1:10
    elev2_matrix(i,:) = elev2_def(i);
    alpha4_matrix(i,:) = alpha4_table;
end;

cm_ade_neg25 = [0.9219,0.8735,0.827,0.7798,0.7413,0.6706,0.587,0.5293,0.4276,0.3045,0.2379,0.1698,0.1088,0.0503,-0.006,-0.0704,-0.1576,-0.2624];

cm_ade_neg20 = [0.8532,0.8031,0.7546,0.7073,0.6698,0.5983,0.5275,0.4822,0.3743,0.252,0.1943,0.1367,0.0721,0.0109,-0.0452,-0.1072,-0.1668,-0.2034];

cm_ade_neg15 = [0.7722,0.7138,0.6717,0.6258,0.5939,0.5384,0.4621,0.4109,0.3178,0.2058,0.1426,0.0705,0.0028,-0.0594,-0.123,-0.1827,-0.2384,-0.2822];

cm_ade_neg10 = [0.6464,0.5998,0.559,0.5156,0.4682,0.4039,0.3265,0.2551,0.1513,0.0404,-0.0216,-0.0911,-0.1512,-0.2198,-0.2834,-0.3425,-0.3938,-0.4234];

cm_ade_neg5 = [0.5099,0.4673,0.4248,0.3705,0.3111,0.2426,0.1675,0.0901,-0.0115,-0.1199,-0.1775,-0.2419,-0.3048,-0.3693,-0.429,-0.4869,-0.5246,-0.5369];

cm_ade_0 = [0.3524,0.305,0.2505,0.1922,0.1331,0.0681,-0.0041,-0.0826,-0.1771,-0.285,-0.3435,-0.4058,-0.4655,-0.5284,-0.583,-0.626,-0.6414,-0.6366];

cm_ade_5 = [0.2042,0.1561,0.1051,0.0403,-0.0198,-0.0833,-0.1556,-0.2472,-0.3376,-0.4361,-0.4882,-0.5455,-0.6097,-0.6657,-0.7117,-0.7409,-0.7367,-0.726];

cm_ade_10 = [0.0376,-0.0146,-0.075,-0.1391,-0.1921,-0.2594,-0.3327,-0.4179,-0.5038,-0.5937,-0.6446,-0.7024,-0.7536,-0.8052,-0.8406,-0.8481,-0.832,-0.8175];

cm_ade_15 = [-0.11,-0.1606,-0.2137,-0.2678,-0.3162,-0.378,-0.4518,-0.5384,-0.6162,-0.6994,-0.7465,-0.8015,-0.8507,-0.903,-0.9229,-0.9051,-0.8863,-0.8832];

cm_ade_20 = [-0.1353,-0.1817,-0.2356,-0.3001,-0.3675,-0.4532,-0.5296,-0.6121,-0.6925,-0.7772,-0.8266,-0.8897,-0.9399,-0.9818,-0.9877,-0.9575,-0.9439,-0.9447];

Cm_fade_lookup = [cm_ade_neg25;cm_ade_neg20;cm_ade_neg15;cm_ade_neg10;cm_ade_neg5;cm_ade_0;cm_ade_5;cm_ade_10;cm_ade_15;cm_ade_20];

Cm_fade = griddata(elev2_matrix,alpha4_matrix,Cm_fade_lookup,de,alpha);
end
function[Cn_fada] = eval_Cn_fada(da,alpha);

% Cn_ada
alpha1_table = linspace(-8,17,26);

ail_def = linspace(-25,25,11);

ail_matrix = zeros(11,26);
alpha1_matrix = zeros(11,26);

for i = 1:11
    ail_matrix(i,:) = ail_def(i);
    alpha1_matrix(i,:) = alpha1_table;
end;




cn_ada_0 = linspace(0,0,26);

cn_ada_5 = [0.0004,0.0002,0.0001,-0.0001,-0.0002,-0.0004,-0.0005,-0.0007,-0.0008,-0.001,-0.0011,-0.0013,-0.0015,-0.0016,-0.0018,-0.0019,-0.0021,-0.0022,-0.0024,-0.0026,-0.0027,-0.0029,-0.003,-0.0032,-0.0034,-0.0035];

cn_ada_10 = [0.0013,0.0009,0.0004,0,-0.0004,-0.0007,-0.0011,-0.0015,-0.0018,-0.0022,-0.0025,-0.0029,-0.0032,-0.0035,-0.0038,-0.0041,-0.0043,-0.0046,-0.0049,-0.0051,-0.0054,-0.0056,-0.0058,-0.006,-0.0062,-0.0064];

cn_ada_15 = [0.0014,0.0008,0.0003,-0.0002,-0.0007,-0.0011,-0.0016,-0.0021,-0.0026,-0.003,-0.0035,-0.004,-0.0044,-0.0049,-0.0053,-0.0057,-0.0062,-0.0066,-0.007,-0.0074,-0.0078,-0.0082,-0.0086,-0.009,-0.0094,-0.0098];

cn_ada_20 = [0.0022,0.0015,0.0008,0.0001,-0.0005,-0.0012,-0.0019,-0.0025,-0.0032,-0.0038,-0.0044,-0.005,-0.0057,-0.0063,-0.0069,-0.0075,-0.0081,-0.0087,-0.0093,-0.0098,-0.0104,-0.011,-0.0115,-0.0121,-0.0126,-0.0132];	

cn_ada_25 = [0.0025,0.0018,0.001,0.0003,-0.0004,-0.0012,-0.0019,-0.0026,0.0033,-0.004,-0.0047,-0.0054,-0.0061,-0.0068,-0.0074,-0.0081,-0.0088,-0.0094,-0.0101,-0.0107,-0.0114,-0.012,-0.0127,-0.0133,-0.0139,-0.0145];

cn_ada_neg5 = -1*cn_ada_5;
cn_ada_neg10 = -1*cn_ada_10;
cn_ada_neg15 = -1*cn_ada_15;
cn_ada_neg20 = -1*cn_ada_20;
cn_ada_neg25 = -1*cn_ada_25;

Cn_fada_lookup = [cn_ada_neg25;cn_ada_neg20;cn_ada_neg15;cn_ada_neg10;cn_ada_neg5;cn_ada_0;cn_ada_5;cn_ada_10;cn_ada_15;cn_ada_20;cn_ada_25];


Cn_fada = griddata(ail_matrix,alpha1_matrix,Cn_fada_lookup,da,alpha);
end
function[Cn_fbdr] = eval_Cn_fbdr(dr,Beta)

%Cn_fbdr
Beta2_table = [-20,-17.5,-15,-12.5,-10,-7.5,-5,-2.5,0,2.5,5,7.5,10,11.25,12.5,13.75,15,16.25,17.5,18.75,20];

rdr2_def = linspace(-25,25,11);

Beta2_matrix = zeros(11,21);
rdr2_matrix = zeros(11,21);

for i=1:11
    
    rdr2_matrix(i,:) = rdr2_def(i);
    Beta2_matrix(i,:) = Beta2_table;
end;

cn_bdr_neg25 = [0.0067,0,0.004,0.0087,0.0121,0.0161,0.02,0.0275,0.0335,0.0382,0.0429,0.0476,0.0516,0.0549,0.0563,0.057,0.0523,0.0503,0.0509,0.0509,0.0509];

cn_bdr_neg20 = [0.0027,0.0013,0.002,0.0074,0.0101,0.0127,0.0174,0.0235,0.0295,0.0348,0.0389,0.0429,0.0462,0.0489,0.0509,0.0509,0.0482,0.0442,0.0428,0.0436,0.0442];

cn_bdr_neg15 = [-0.0027,-0.0054,-0.004,0.0027,0.0054,0.0087,0.0134,0.0188,0.0255,0.0288,0.0328,0.0375,0.0422,0.0442,0.0462,0.0469,0.0429,0.0415,0.0422,0.0429,0.0429];

cn_bdr_neg10 = [-0.0094,-0.0127,-0.0114,-0.0047,-0.0014,0.0013,0.0067,0.0127,0.0188,0.0228,0.0261,0.0288,0.0348,0.0375,0.0395,0.0402,0.0369,0.0355,0.0355,0.0362,0.0369];

cn_bdr_neg5 = [-0.0154,-0.0214,-0.02,-0.0134,-0.0101,-0.0064,-0.002,0.0047,0.0114,0.0154,0.0121,0.0248,0.0295,0.0315,0.0335,0.0342,0.0322,0.0308,0.0308,0.0308,0.0322];

cn_bdr_0 = [-0.0214,-0.0277,-0.0283,-0.0222,-0.0188,-0.0161,-0.0131,-0.0067,0,0.006,0.0101,0.0154,0.0188,0.0201,0.0235,0.0268,0.0255,0.0255,0.0235,0.0235,0.0248];

cn_bdr_5 = [-0.0274,-0.034,-0.0366,-0.031,-0.0275,-0.0258,-0.0242,-0.0181,-0.0074,-0.0034,0.0081,0.006,0.0081,0.0087,0.0135,0.0194,0.0188,0.0202,0.0162,0.0162,0.0174];

cn_bdr_10 = [-0.0334,-0.0427,-0.0452,-0.0397,-0.0362,-0.0335,-0.0329,-0.0261,-0.0148,-0.0108,-0.0059,0.002,0.0028,0.0027,0.0075,0.0134,0.0141,0.0155,0.0115,0.0108,0.0127];

cn_bdr_15 = [-0.0401,-0.05,-0.0526,-0.0471,-0.043,-0.0409,-0.0396,-0.0322,-0.0215,-0.0168,-0.0126,-0.0067,-0.0046,-0.004,0.0008,0.0067,0.0081,0.0095,0.0048,0.0041,0.0067];

cn_bdr_20 = [-0.0455,-0.0567,-0.586,-0.0052,-0.0477,-0.0449,-0.0436,-0.0369,-0.0255,-0.0228,-0.0187,-0.0121,-0.0086,-0.0088,-0.0039,0.0027,0.0001,0.0068,0.0042,0.0034,0.0054];

cn_bdr_25 = [-0.0495,-0.0554,-0.0606,-0.0531,-0.0497,-0.0483,-0.0462,-0.0409,-0.0295,-0.0262,-0.0227,-0.0168,-0.014,-0.0147,-0.0093,-0.0034,-0.0013,0.0007,-0.0039,-0.0039,-0.0013];

Cn_fbdr_lookup = [cn_bdr_neg25;cn_bdr_neg20;cn_bdr_neg15;cn_bdr_neg10;cn_bdr_neg5;cn_bdr_0;cn_bdr_5;cn_bdr_10;cn_bdr_15;cn_bdr_20;cn_bdr_25];

Cn_fbdr = griddata(rdr2_matrix,Beta2_matrix,Cn_fbdr_lookup,dr,Beta);

end
function[CY_fbdr] = eval_CY_fbdr(dr,Beta);

% CY_fbdr
Beta_table = [-20,0,20];
Beta_matrix = zeros(3,3);

rdr_def = [-20,0,20];
rdr_matrix = zeros(3,3);

for i = 1:3
Beta_matrix(i,:) = Beta_table;
rdr_matrix(i,:) = rdr_def(i);
end


cy_bdr_neg20 = [0.19,0.24,0.29];
cy_bdr_0 = [-0.05,0,0.05];
cy_bdr_20 = [-0.25,-0.22,-0.18];

CY_fbdr_lookup = [cy_bdr_neg20;cy_bdr_0;cy_bdr_20];


CY_fbdr = griddata(rdr_matrix,Beta_matrix,CY_fbdr_lookup, dr, Beta);

end